package com.usian.article.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.fegin.BehaviorFegin;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.service.ApArticleService;
import com.usian.common.constants.article.ArticleConstans;
import com.usian.model.article.ApArticle;
import com.usian.model.article.ApCollection;
import com.usian.model.article.ArticleHomeDto;
import com.usian.model.article.CollectionBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;




@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Value("${fdfs.url}")
    String fileServerUrl;

    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //参数校验
        Integer size = dto.getSize();
        if (size == null || size ==0){
            size=10;
        }
        //校验size是否超过最大数,超过最大数 就让size等于最大数
        size = Math.min(size, MAX_PAGE_SIZE);
        dto.setSize(size);

        //校验类型参数
        if (!loadtype.equals(ArticleConstans.LOADTYPE_LOAD_MORE) && !loadtype.equals(ArticleConstans.LOADTYPE_LOAD_NEW)){
            loadtype = ArticleConstans.LOADTYPE_LOAD_MORE;
        }
        //如果没有指定频道类型就查全部
        //检查频道参数
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstans.DEFAULT_TAG);
        }
        //对时间参数进行校验
        if (dto.getMaxBehotTime()==null) dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime()==null) dto.setMinBehotTime(new Date());
        //查询
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadtype);
        //结果封装
        ResponseResult result = ResponseResult.okResult(apArticles);
        result.setHost(fileServerUrl);
        return result;
    }


}
